package org.i1510.common.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.dao.ISysPopDao;
import org.i1510.common.po.SysPop;
import org.i1510.common.service.ISysPopService;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.StringUtils;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("iSysPopService")
public class SysPopServiceImpl extends BaseTreeServiceImpl implements
		ISysPopService {
	@Autowired
	private ISysPopDao iSysPopDao;

	
	public ResultMapper saveList(List<Map<String,String>> list, HttpServletRequest request){
		ResultMapper mapper = ResultMapper.generateSuccess();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			Map<String,String> map = list.get(i);
			SysPop pop = new SysPop();
			pop.setDocId(map.get("docId"));
			pop.setDescs(map.get("descs"));
			pop.setDocStatus(map.get("docStatus"));
			pop.setDocSubject(map.get("docSubject"));
			pop.setIsMenu(map.get("isMenu"));
			pop.setIsValid(map.get("isVaild"));
			pop.setJavaClass(map.get("javaClass"));
			pop.setJavaMethod(map.get("javaMethod"));
			pop.setUrl(map.get("url"));
			pop.setIcon(map.get("icon"));
			pop.setFdOrder(StringUtils.String2Integer(map.get("fdOrder")));
			//获取上级信息
			SysPop p = iSysPopDao.findById(map.get("parentId"));
			if(null != p){
				pop.setParentId(p.getDocId());
				pop.setParentName(p.getDocSubject());
			}else{
				pop.setParentId("0000");
				pop.setParentName("根目录");
				
			}
			sb.append("docId:"+map.get("docId")+"执行"+saveIfNull4Excel(pop, request).getMap().get("success"));
			
		}
		mapper.appendResult("message", sb.toString());
		
		return mapper;
	}
	public List getMenu(String fdRoleId) {
		Map map = new HashMap();
		map.put("fdRoleId",fdRoleId);
		List list = this.queryForListBySql("sysPopSql.getMenu", map);
		return this.getTree("0000", list, "parentId");
	}
	public List getAll(SysPop sysPop, Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(sysPop, map);
		map.putAll(conditions);
		List list = this.queryForListBySql("sysPopSql.getAll", map);
		return this.getTree(conditions.get("parentId") == null ? "0000"
				: conditions.get("parentId").toString(), list, "parentId");
	}

	/**
	*@Author Rambo
	*@Desc：如果找不到则创建或者为更新
	*@param basePo
	*@param request
	*@return TODO
	*@Version  ISysPopService.java,v 1.1 2016年7月18日 上午10:54:29 
	*/
	private ResultMapper saveIfNull4Excel(SysPop basePo, HttpServletRequest request) {
		ResultMapper mapper = ResultMapper.generateFailure();
		SysPop saved = null;
		if (null != basePo && basePo.getDocId() != null
				&& !basePo.getDocId().equals("")) {// 修改
			SysPop finder = iSysPopDao.findById(basePo.getDocId());
			if (null != finder) {
				finder.setDocLastTime(DateUtils.getNow());
				finder.setDocLastor(UserUtils.getSessionUserBean(request)
						.getSysUser().getDocId());
				BeanUtils.copy(finder, basePo);
				saved = iSysPopDao.save(finder);
			} else {
				basePo.setDocId(basePo.getDocId());
				basePo.setDocCreateTime(DateUtils.getNow());
				basePo.setDocCreator(UserUtils.getSessionUserBean(request)
						.getSysUser().getDocId());
				saved = iSysPopDao.save(basePo);
			}

		} else {// 新增
			basePo.setDocId(basePo.getDocId());
			basePo.setDocCreateTime(DateUtils.getNow());
			basePo.setDocCreator(UserUtils.getSessionUserBean(request)
					.getSysUser().getDocId());
			saved = iSysPopDao.save(basePo);
		}

		if (saved == null) {
			mapper = ResultMapper.generateFailure();
			return mapper;
		} else {
			mapper = ResultMapper.generateSuccess();
			mapper.appendResult("docId", saved.getDocId());
			return mapper;
		}
	}
	public ResultMapper save(SysPop basePo, HttpServletRequest request) {
		ResultMapper mapper = ResultMapper.generateFailure();
		SysPop saved = null;
		if (null != basePo && basePo.getDocId() != null
				&& !basePo.getDocId().equals("")) {// 修改
			SysPop finder = iSysPopDao.findById(basePo.getDocId());
			if (null != finder) {
				finder.setDocLastTime(DateUtils.getNow());
				finder.setDocLastor(UserUtils.getSessionUserBean(request)
						.getSysUser().getDocId());
				BeanUtils.copy(finder, basePo);
				saved = iSysPopDao.save(finder);
			} else {
				mapper = ResultMapper.generateFailure();
				return mapper;
			}

		} else {// 新增
			basePo.setDocId(IDGenerator.getGenerateID());
			basePo.setDocCreateTime(DateUtils.getNow());
			basePo.setDocCreator(UserUtils.getSessionUserBean(request)
					.getSysUser().getDocId());
			saved = iSysPopDao.save(basePo);
		}

		if (saved == null) {
			mapper = ResultMapper.generateFailure();
			return mapper;
		} else {
			mapper = ResultMapper.generateSuccess();
			mapper.appendResult("docId", saved.getDocId());
			return mapper;
		}
	}

	public ResultMapper delete(String docId) {

		ResultMapper mapper = ResultMapper.generateFailure();
		iSysPopDao.delete(docId);
		mapper = ResultMapper.generateSuccess();
		return mapper;
	}

	public SysPop findById(String docId) {
		return iSysPopDao.findById(docId);
	}

	public List<SysPop> findByIds(List<String> docIds) {
		return (List<SysPop>) iSysPopDao.findAll(docIds);
	}

	public List getPage(SysPop sysPop, PageBean pageBean, Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(sysPop, map);
		map.put("intPageIndex", pageBean.getIntPageIndex());
		map.put("intPageSize", pageBean.getIntPageSize());
		map.putAll(conditions);
		List list = this.queryForListBySql("sysPopSql.getPage", map);
		return list;
	}

	public int getPageCount(SysPop sysPop, Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(sysPop, map);
		map.putAll(conditions);
		List list = this.queryForListBySql("sysPopSql.getPageCount", map);
		return list.size();
	}

	@Transactional
	public ResultMapper saveAll(List<SysPop> list) {

		ResultMapper mapper = ResultMapper.generateFailure();

		List<SysPop> listSaved = iSysPopDao.save(list);

		if (listSaved == null) {
			mapper = ResultMapper.generateFailure();
			return mapper;
		}
		mapper = ResultMapper.generateSuccess();
		return mapper;
	}

	public List getSysUser4Pop(Map conditions) {
		Map map = new HashMap();
		map.putAll(conditions);
		List list = this.queryForListBySql("sysPopSql.getSysUser4Pop", map);
		return list;
	}
}